package com.zenithmind.chat.controller;

import com.zenithmind.common.annotation.Log;
import com.zenithmind.common.constant.OperationTypeEnum;
import com.zenithmind.chat.pojo.entity.ModelConfig;
import com.zenithmind.chat.pojo.query.ModelConfigQuery;
import com.zenithmind.chat.service.ModelConfigService;
import com.zenithmind.common.result.PageResult;
import com.zenithmind.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@Tag(name = "AI模型配置接口")
@RestController
@RequestMapping("/api/zenithMind/modelConfig")
public class ModelConfigController {

    private final ModelConfigService modelConfigService;

    /**
     * 构造函数注入 - 遵循依赖倒置原则
     */
    public ModelConfigController(ModelConfigService modelConfigService) {
        this.modelConfigService = modelConfigService;
    }

    /**
     * 获取所有可用模型配置
     *
     * @return 模型配置列表
     */
    @Log(description = "获取所有可用模型配置", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "获取所有可用模型配置")
    @GetMapping("/getAllEnabledModels")
    public Result<List<ModelConfig>> getAllEnabledModels() {
        log.info("获取所有可用模型配置");
        return Result.success(modelConfigService.getAllEnabledModels());
    }

    /**
     * 获取指定类型的所有可用模型配置
     *
     * @param modelType 模型类型
     * @return 模型配置列表
     */
    @Log(description = "获取指定类型的所有可用模型配置", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "获取指定类型的所有可用模型配置")
    @GetMapping("/getEnabledModelsByType/{modelType}")
    public Result<List<ModelConfig>> getEnabledModelsByType(@PathVariable Integer modelType) {
        log.info("获取类型为{}的所有可用模型配置", modelType);
        return Result.success(modelConfigService.getEnabledModelsByType(modelType));
    }

    /**
     * 获取模型配置详情
     *
     * @param id 配置ID
     * @return 模型配置
     */
    @Log(description = "获取模型配置详情", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "获取模型配置详情")
    @GetMapping("/getModelConfig/{id}")
    public Result<ModelConfig> getModelConfig(@PathVariable Long id) {
        log.info("获取模型配置详情: {}", id);
        return Result.success(modelConfigService.getConfigById(id));
    }

    /**
     * 新增或更新模型配置
     *
     * @param modelConfig 模型配置
     * @return 操作结果
     */
    @Log(description = "新增或更新模型配置", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "新增或更新模型配置")
    @PostMapping("/saveOrUpdateModelConfig")
    public Result<Boolean> saveOrUpdateModelConfig(@RequestBody ModelConfig modelConfig) {
        log.info("新增或更新模型配置: {}", modelConfig);
        return Result.success(modelConfigService.saveOrUpdateConfig(modelConfig));
    }

    /**
     * 删除模型配置
     *
     * @param id 配置ID
     * @return 删除结果
     */
    @Log(description = "删除模型配置", operationType = OperationTypeEnum.DELETE)
    @Operation(summary = "删除模型配置")
    @DeleteMapping("/deleteModelConfig/{id}")
    public Result<Boolean> deleteModelConfig(@PathVariable Long id) {
        log.info("删除模型配置: {}", id);
        return Result.success(modelConfigService.deleteConfig(id));
    }

    /**
     * 更新模型启用状态
     *
     * @param id 配置ID
     * @param enabled 启用状态 (0: 禁用, 1: 启用)
     * @return 更新结果
     */
    @Log(description = "更新模型启用状态", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "更新模型启用状态")
    @PutMapping("/{id}/status")
    public Result<Boolean> updateModelStatus(@PathVariable Long id, @RequestParam Integer enabled) {
        log.info("更新模型启用状态: id={}, enabled={}", id, enabled);
        return Result.success(modelConfigService.updateModelStatus(id, enabled));
    }

    /**
     * 分页获取模型配置
     *
     * @param query 查询参数
     * @return 分页数据
     */
    @Log(description = "分页获取模型配置", operationType = OperationTypeEnum.QUERY)
    @Operation(summary = "分页获取模型配置")
    @GetMapping("/getModelConfigPage")
    public Result<PageResult<ModelConfig>> getModelConfigPage(ModelConfigQuery query) {
        log.info("分页获取模型配置: query={}", query);
        return Result.success(modelConfigService.getModelPage(query));
    }

    /**
     * 批量删除模型配置
     *
     * @param ids 配置ID列表
     * @return 删除结果
     */
    @Log(description = "批量删除模型配置", operationType = OperationTypeEnum.DELETE)
    @Operation(summary = "批量删除模型配置")
    @DeleteMapping("/deleteModelConfigBatch")
    public Result<Boolean> deleteModelConfigBatch(@RequestBody List<Long> ids) {
        log.info("批量删除模型配置: {}", ids);
        return Result.success(modelConfigService.deleteBatch(ids));
    }

    /**
     * 批量新增或更新模型配置
     *
     * @param modelConfigs 模型配置列表
     * @return 操作结果
     */
    @Log(description = "批量新增或更新模型配置", operationType = OperationTypeEnum.UPDATE)
    @Operation(summary = "批量新增或更新模型配置")
    @PostMapping("/saveOrUpdateModelConfigBatch")
    public Result<Boolean> saveOrUpdateModelConfigBatch(@RequestBody List<ModelConfig> modelConfigs) {
        log.info("批量新增或更新模型配置: {}", modelConfigs);
        return Result.success(modelConfigService.saveOrUpdateBatchConfig(modelConfigs));
    }
} 